/**
 * 
 */
package neptune.manager.configurator;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import neptune.manager.model.topology.IPAddress;
import neptune.manager.utils.SSHCommandException;
import neptune.manager.utils.SSHConnectionHandler;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class SSHCommandsTEST {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// String ip = "192.1.12.4";
		// System.out.println(ip.matches(".+\\..+\\..+\\..+"));
		//
		// System.out.println(SSHCommandsUtils.getDelRouteCommand(new String[] {
		// ip, "*", "255.0.0.0", "U", "0", "0", "0", "eht0" }));

		System.out.println("--- STARTING ROUTE SET UP TEST ---");
		test();
		
//		IPAddress subnet = new IPAddress(4, "192.168.0.0", "255.255.255.0");
//		System.out.println(subnet.include("192.168.0.0"));
	}

	private static void test() throws Exception {
		System.out.print("Connection...");
		SSHConnectionHandler handler = SSHConnectionHandler
				.createSSHConnectionHandler("192.168.0.5", 22, "root",
						"224RoBivath");
		System.out.println("\t\t[OK]");

		System.out.print("Getting routes... ");
		String[][] routes = getNodeRoutes(handler);
		System.out.println("\t\t[OK]");

		for (String[] strings : routes) {
			System.out.println(Arrays.toString(strings));
		}

		for (String[] route : routes) {
			System.out.println("Address handling: " + route[0]);
			String destAddress = route[0];
			boolean isControl = false;

			IPAddress subnet = new IPAddress(4, "192.168.0.0", "255.255.255.0");

			System.out.println("Control Subnet: " + subnet);
			System.out.print("Is route on control subnet...");
			if (destAddress.matches(".+\\..+\\..+\\..+")) {
				if (subnet.include(destAddress)) {
					System.out.println("\t\tYES");
					isControl = true;
				} else 
					System.out.println("\t\tNO");
			} else 
				System.out.println("\t\tNO");

			if (!isControl) {
				System.out.println("Sending route del command: "
						+ SSHCommandsUtils.getDelRouteCommand(route));
				String command = SSHCommandsUtils.getDelRouteCommand(route);
				System.out.print("Sending command...");
				handler.sendCommand(command);
				System.out.println("\t\t[OK]");
			}
		}

	}

	private static String[][] getNodeRoutes(SSHConnectionHandler handler)
			throws SSHCommandException {
		handler.sendCommand(SSHCommandsUtils.getRouteCommand());

		BufferedReader reader = handler.getStdOut() ;
		String line;

		int i = 0;
		try {

			ArrayList<String[]> routes = new ArrayList<String[]>(0);

			while ((line = reader.readLine()) != null) {
				if (i > 1) { // elimina le prime due righe restituite
					// dall'output
					String[] result = new String[8];
					String[] splitRes = line.split(" ");
					int j = 0;
					for (String split : splitRes) {
						if (!split.equals("")) {
							result[j] = split;
							j++;
						}
					}
					routes.add(result);
				}
				i++;
			}

			return routes.toArray(new String[][] {});
		} catch (IOException e) {
			throw new SSHCommandException(e);
		}
	}
}
